<?php 
ob_start();
session_start();
$teamid=$_SESSION['teamID'];
if ($_SESSION['teamID']!=1){header('LOCATION:main.php');}
?>
<!DOCTYPE html>
<html>
<head>
<title>Match Result</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/bootstrap-datepicker3.css" rel="stylesheet" />
<link href="Content/site.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/bootstrap.js"></script>
<script src="Scripts/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="js/form_validation.js"></script>
<?php $scheduleID=$_GET['id'];?>
<?php include_once("php/coach_manage_result_classes.php");?>
</head>
<body>
<div class="container blue_border">
<?php 
include_once("php/banner.php");
?>
<?php 
include_once("php/nav_bar.php");
?>
<div class="row well text-center bgF3F5FC">
  <?php include("php/coach_manage_result_variables.php");?>
  <h2 class="text253A7D"> <?php echo "Round ".$round->round_number." (".$round->round_date.")";?></h2>
    <h3>
  	<?php $winLostTeamMatches=$repCalc->countTeamWonMatches($scheduleID);
			if ( $winLostTeamMatches['winTeam1']>$winLostTeamMatches['winTeam2'])
			echo "<span class='score-win'>";
			else 
			echo "<span class='score-loose'>";
			echo $winLostTeamMatches['winTeam1']."</span>"?>
            &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;
			<?php 
			if ( $winLostTeamMatches['winTeam2']>$winLostTeamMatches['winTeam1'])
			echo "<span class='score-win'>";
			else 
			echo "<span class='score-loose'>";
			echo $winLostTeamMatches['winTeam2']."</span>";?>&nbsp;
  </h3>
</div>
<div class="row">
<div class="container-fluid">
  <div class="row">
    <div class="col-md-10 col-xs-12 col-md-offset-1">
    <form action="php/update_players.php" role="form" method="post" class="form" name="updatePlayers">
    <input type="hidden" name="scaduleId" id="scaduleId" value="<?php echo $scheduleID; ?>" />
      <table class="table table-striped table-bordered align-center ">
        <thead>
          <tr>
            <th>ABC</th>
            <th><?php echo $team1->team_name;?></th>
            <th></th>
            <th>XYZ</th>
            <th><?php echo $team2->team_name;?></th> 
          </tr>
        </thead>
        <tbody>
          <tr>
          	<?php $player=$repPlayer->map($repPlayer->readPlayer($players1[0]));?>
          	<td>
            	A <input type="radio" name="player1T1Letter" value="A" id="A1" <?php if ($player->playerID==$matches[0]->player1ID) echo "checked";  ?>> 
                B <input type="radio" name="player1T1Letter" value="B" id="B1" <?php if ($player->playerID==$matches[1]->player1ID) echo "checked";  ?>>
                C <input type="radio" name="player1T1Letter" value="C" id="C1" <?php if ($player->playerID==$matches[2]->player1ID) echo "checked";  ?>>
            </td>
            <td class="align-left">
            
            <input type="hidden" name="player1T1_old" id="player1T1_old" value="<?php echo $player->playerID; ?>" />
			<select class="form-control" name="player1T1" id="player1T1">
                <optgroup class="option-team">
                    <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                    <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[1])); ?>
                    <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                    <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[2])); ?>
                    <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                </optgroup>
           <?php 
			foreach ($players as $p){?>
            <option value="<?php echo $p->playerID; ?>"><?php echo ($p->fname." ".$p->sname); ?></option>
			<?php } ?>
            </select>
            </td>
            <td rowspan="3" class="vs-inner">VS</td>
            <td>
            <?php $player=$repPlayer->map($repPlayer->readPlayer($players2[0])); ?>
            	X <input type="radio" name="player1T2Letter" value="X" id="X1" <?php if ($player->playerID==$matches[0]->player2ID) echo "checked";  ?>>
                Y <input type="radio" name="player1T2Letter" value="Y" id="Y1" <?php if ($player->playerID==$matches[1]->player2ID) echo "checked";  ?>>
                Z <input type="radio" name="player1T2Letter" value="Z" id="Z1" <?php if ($player->playerID==$matches[2]->player2ID) echo "checked";  ?>>
                
            </td>
            <td class="align-left">
            	
                <input type="hidden" name="player1T2_old" id="player1T2_old" value="<?php echo $player->playerID; ?>" />
				<select class="form-control" name="player1T2" id="player1T2">
                	<optgroup class="option-team">
                        <option  value="<?php echo $player->playerID; ?>"><?php echo $player->fname." ".$player->sname; ?></option>
                         <?php $player=$repPlayer->map($repPlayer->readPlayer($players2[1])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                        <?php $player=$repPlayer->map($repPlayer->readPlayer($players2[2])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                   </optgroup>
				   <?php 
				   
                    foreach ($players as $p){?>
                    <option value="<?php echo $p->playerID; ?>"><?php echo ($p->fname." ".$p->sname); ?></option>
                    <?php } ?>
            	</select>
			</td>
            
          </tr>
          <tr>
            <td>
            <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[1])); ?>
            	A <input type="radio" name="player2T1Letter" value="A" id="A2" <?php if ($player->playerID==$matches[0]->player1ID) echo "checked";  ?>>
                B <input type="radio" name="player2T1Letter" value="B" id="B2" <?php if ($player->playerID==$matches[1]->player1ID) echo "checked";  ?>>
                C <input type="radio" name="player2T1Letter" value="C" id="C2" <?php if ($player->playerID==$matches[2]->player1ID) echo "checked";  ?>>
                
            </td>
            <td class="align-left">
            	
                <input type="hidden" name="player2T1_old" id="player2T1_old" value="<?php echo $player->playerID; ?>" />
                <select class="form-control" name="player2T1" id="player2T1">
                	<optgroup class="option-team">
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                         <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[0])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                        <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[2])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                    </optgroup>
                    <?php 
                    foreach ($players as $p){?>
                    <option value="<?php echo $p->playerID; ?>"><?php echo ($p->fname." ".$p->sname); ?></option>
                    <?php } ?>
                </select>
            </td>
            <td>
           		<?php $player=$repPlayer->map($repPlayer->readPlayer($players2[1])); ?>
            	X <input type="radio" name="player2T2Letter" value="X" id="X2" <?php if ($player->playerID==$matches[0]->player2ID) echo "checked";  ?>>
                Y <input type="radio" name="player2T2Letter" value="Y" id="Y2" <?php if ($player->playerID==$matches[1]->player2ID) echo "checked";  ?>>
                Z <input type="radio" name="player2T2Letter" value="Z" id="Z2" <?php if ($player->playerID==$matches[2]->player2ID) echo "checked";  ?>>
                
            </td>
            <td class="align-left">
            	
                <input type="hidden" name="player2T2_old" id="player2T2_old" value="<?php echo $player->playerID; ?>" />
				<select class="form-control" name="player2T2" id="player2T2">
                	<optgroup class="option-team">
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                         <?php $player=$repPlayer->map($repPlayer->readPlayer($players2[0])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                        <?php $player=$repPlayer->map($repPlayer->readPlayer($players2[2])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                    </optgroup>
                   <?php 
                    foreach ($players as $p){?>
                    <option value="<?php echo $p->playerID; ?>"><?php echo ($p->fname." ".$p->sname); ?></option>
                    <?php } ?>
            	</select>            
            </td>

          </tr>
          <tr>
          	<td>
             <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[2]));?>
            	A <input type="radio" name="player3T1Letter" data-col="A" value="A" id="A" <?php if ($player->playerID==$matches[0]->player1ID) echo "checked";  ?>>
                B <input type="radio" name="player3T1Letter" data-col="B" value="B" id="B" <?php if ($player->playerID==$matches[1]->player1ID) echo "checked";  ?>>
                C <input type="radio" name="player3T1Letter" data-col="C" value="C" id="C" <?php if ($player->playerID==$matches[2]->player1ID) echo "checked";  ?>>
                
            </td>
            <td class="align-left">
           
            <input type="hidden" name="player3T1_old" id="player3T1_old" value="<?php echo $player->playerID; ?>" />
               <select class="form-control" name="player3T1" id="player3T1">
               		<optgroup class="option-team">
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                         <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[1])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                        <?php $player=$repPlayer->map($repPlayer->readPlayer($players1[0])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                    </optgroup>
                    <?php 
                    foreach ($players as $p){?>
                    <option value="<?php echo $p->playerID; ?>"><?php echo ($p->fname." ".$p->sname); ?></option>
                    <?php } ?>
                </select>
            </td>
            <td>
            	<?php $player=$repPlayer->map($repPlayer->readPlayer($players2[2]));?>
            	X <input type="radio" name="player3T2Letter" value="X" id="X3" <?php if ($player->playerID==$matches[0]->player2ID) echo "checked";  ?>>
                Y <input type="radio" name="player3T2Letter" value="Y" id="Y3" <?php if ($player->playerID==$matches[1]->player2ID) echo "checked";  ?>>
                Z <input type="radio" name="player3T2Letter" value="Z" id="Z3" <?php if ($player->playerID==$matches[2]->player2ID) echo "checked";  ?>>
            </td>
            <td class="align-left">
            	
                <input type="hidden" name="player3T2_old" id="player3T2_old" value="<?php echo $player->playerID; ?>" />
                <select class="form-control" name="player3T2" id="player3T2">
                	<optgroup class="option-team">
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                         <?php $player=$repPlayer->map($repPlayer->readPlayer($players2[1])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                        <?php $player=$repPlayer->map($repPlayer->readPlayer($players2[0])); ?>
                        <option value="<?php echo $player->playerID; ?>"> <?php echo $player->fname." ".$player->sname; ?></option>
                    </optgroup>
                    <?php 
                    foreach ($players as $p){?>
                    <option value="<?php echo $p->playerID; ?>"><?php echo ($p->fname." ".$p->sname); ?></option>
                    <?php } ?>
                </select>           
            </td>
            
          </tr>
          <tr>
          <td colspan="5"><button class="btn btn-default btn-lg" type="submit" name="submit" onclick = "return  validatePlayerUpdate(this.updatePlayers)">Populate</button></td>
		  </tr>
        </tbody>
      </table>


<div class="row">
<div class="container-fluid">
  <div class="row">
    <div class="col-md-10 col-xs-12 col-md-offset-1">
      <table class="table table-striped table-bordered align-center">
        <thead>
          <tr>
            <th><?php echo $team1->team_name;?></th>
            <th>Won</th>
            <th>Lost</th>
            <th></th>
            <th><?php echo $team2->team_name;?></th>
            <th>Won</th>
            <th>Lost</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="align-left"><?php $player=$repPlayer->map($repPlayer->readPlayer($players1[0])); echo $player->fname." ".$player->sname; ?></td>
            <td>
            <?php $winLostMatches=$repCalc->countWonMatches($scheduleID,$player->playerID); 
			echo $winLostMatches['win'];?>
            </td>
            <td><?php echo $winLostMatches['lost']; ?> </td>
            <td rowspan="3" class="vs-inner">VS</td>
            <td class="align-left"><?php $player=$repPlayer->map($repPlayer->readPlayer($players2[0])); echo $player->fname." ".$player->sname; ?></td>
             <td>
            <?php $winLostMatches=$repCalc->countWonMatches($scheduleID,$player->playerID); 
			echo $winLostMatches['win'];?>
            </td>
            <td><?php echo $winLostMatches['lost']; ?> </td>
            
          </tr>
          <tr>
            <td class="align-left"><?php $player=$repPlayer->map($repPlayer->readPlayer($players1[1])); echo $player->fname." ".$player->sname; ?></td>
             <td>
            <?php $winLostMatches=$repCalc->countWonMatches($scheduleID,$player->playerID); 
			echo $winLostMatches['win'];?>
            </td>
            <td><?php echo $winLostMatches['lost']; ?> </td>
            
            <td class="align-left"><?php $player=$repPlayer->map($repPlayer->readPlayer($players2[1])); echo $player->fname." ".$player->sname; ?></td>
             <td>
            <?php $winLostMatches=$repCalc->countWonMatches($scheduleID,$player->playerID); 
			echo $winLostMatches['win'];?>
            </td>
            <td><?php echo $winLostMatches['lost']; ?> </td>
          </tr>
          <tr>
            <td class="align-left"><?php $player=$repPlayer->map($repPlayer->readPlayer($players1[2])); echo $player->fname." ".$player->sname; ?></td>
             <td>
            <?php $winLostMatches=$repCalc->countWonMatches($scheduleID,$player->playerID); 
			echo $winLostMatches['win'];?>
            </td>
            <td><?php echo $winLostMatches['lost']; ?> </td>
            
            <td class="align-left"><?php $player=$repPlayer->map($repPlayer->readPlayer($players2[2])); echo $player->fname." ".$player->sname; ?></td>
             <td>
            <?php $winLostMatches=$repCalc->countWonMatches($scheduleID,$player->playerID); 
			echo $winLostMatches['win'];?>
            </td>
            <td><?php echo $winLostMatches['lost']; ?> </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>

      </form>
    </div>
  </div>
      <div class="container-fluid">
          <form action="php/update_result.php" role="form" method="post" class="form">
    		<input type="hidden" name="scaduleId" id="scaduleId" value="<?php echo $scheduleID; ?>" />
        <div class="row">
          <div class="col-md-12 no-padding">
            <table class="table table-striped table-bordered align-center">
              <thead>
                <tr>
                  <th></th>
                  <th><?php echo $team1->team_name;?></th>
                  <th>1</th>
                  <th >2</th>
                  <th >3</th>
                  <th></th>
                  <th><?php echo $team2->team_name;?></th>
                  <th >1</th>
                  <th >2</th>
                  <th >3</th>
                  <th colspan="2" >Matches</th>
                  <th colspan="2" >Games</th>
                  <th colspan="2" >Points</th>
                </tr>
              </thead>
              <tbody>
                <?php $j=0; foreach($matches as $m){$j++;?>
                <input type="hidden" name="<?php echo "matchID_".$j;?>" id="<?php echo "matchID_".$j;?>" value="<?php echo $m->matchID;?>" />
                <tr>
                  <td><?php echo $m->player1_letter;?></td>
                  <td class="align-left width14"><?php $player=$repPlayer->map($repPlayer->readPlayer($m->player1ID));
					echo $player->fname." ".$player->sname;?></td>
                  <td class="width7"><input type="text" class="form-control inputwidth24" name="<?php echo "g1player1_".$m->matchID;?>" id="<?php echo "g1player1_".$m->matchID;?>" value="<?php if ($repGame->readGame($m->matchID,1)!=""){$game =$repGame->map($repGame->readGame($m->matchID,1));echo $game->player1Score;}?>" /></td>
                  <td class="width7"><input type="text" class="form-control inputwidth24" name="<?php echo "g2player1_".$m->matchID;?>" id="<?php echo "g2player1_".$m->matchID;?>" value="<?php if ($repGame->readGame($m->matchID,2)!=""){$game =$repGame->map($repGame->readGame($m->matchID,2));
						echo $game->player1Score;}?>" /></td>
                  <td class="width7"><input type="text" class="form-control inputwidth24" name="<?php echo "g3player1_".$m->matchID;?>" id="<?php echo "g3player1_".$m->matchID;?>" value="<?php if ($repGame->readGame($m->matchID,3)!=""){
				  $game =$repGame->map($repGame->readGame($m->matchID,3));
						echo $game->player1Score;}?>" /></td>
                  <td><?php echo $m->player2_letter;?></td>
                  <td class="align-left width14"><?php  $player=$repPlayer->map($repPlayer->readPlayer($m->player2ID));
					echo $player->fname." ".$player->sname;?></td>
                  <td class="width7"><input type="text" class="form-control inputwidth24" name="<?php echo "g1player2_".$m->matchID;?>" id="<?php echo "g1player2_".$m->matchID;?>" value="<?php if ($repGame->readGame($m->matchID,1)!=""){$game =$repGame->map($repGame->readGame($m->matchID,1));
						echo $game->player2Score;}?>" /></td>
                  <td class="width7"><input type="text" class="form-control inputwidth24" name="<?php echo "g2player2_".$m->matchID;?>" id="<?php echo "g2player2_".$m->matchID;?>" value="<?php if ($repGame->readGame($m->matchID,2)!=""){$game =$repGame->map($repGame->readGame($m->matchID,2));
						echo $game->player2Score;}?>" /></td>
                  <td class="width7"><input type="text" class="form-control inputwidth24" name="<?php echo "g3player2_".$m->matchID;?>" id="<?php echo "g3player2_".$m->matchID;?>" value="<?php  if ($repGame->readGame($m->matchID,3)!=""){$game =$repGame->map($repGame->readGame($m->matchID,3));
						echo $game->player2Score;}?>" /></td>
                  <td class="width5">
                  <?php $playersMatch=$repCalc->wonMatch($m->matchID); 
				  echo $playersMatch['player1Match']; ?>
                  </td>
                  <td  class="width5"><?php  echo $playersMatch['player2Match']; ?></td>
                  <td  class="width5">
                  <?php 
				  $playersGames=$repCalc->wonGamesByMatch($m->matchID); 
				  echo $playersGames['player1Games']; ?>
                  </td>
                  <td  class="width5"><?php echo $playersGames['player2Games']; ?></td>
                  <td  class="width5">
				  <?php $playersTotalScore=$repCalc->calcPlayerScore($m->matchID); 
				  echo $playersTotalScore['player1TotalScore']; ?>
                  </td>
                  <td  class="width5"><?php echo $playersTotalScore['player2TotalScore']; ?></td>
                </tr>
                <?php  }?>
                <tr>
                <td></td>
                <td class="align-left width14">Doubles 1</td>
                <?php for ($i=0;$i<3;$i++){?>
                <td  class="width7"> <input type="text" class="form-control inputwidth24" id="team1Points<?php echo $i;?>" name="team1Points<?php echo $i;?>" value="<?php echo $doubles[$i]->team1Points; ?>" /></td>
                <?php }?>
                <td></td>
                <td class="align-left width14">Doubles 1</td>
                <?php for ($i=0;$i<3;$i++){?>
                <td  class="width7"><input type="text" class="form-control inputwidth24" id="team2Points<?php echo $i;?>" name="team2Points<?php echo $i;?>" value="<?php echo $doubles[$i]->team2Points; ?>" /></td>
                <?php }	?>
                <td rowspan="2" class="align-middle">
                	<?php echo $doublesMatches['team1WonDoubles']; ?>
                </td>
                <td rowspan="2" class="align-middle">
                	<?php echo $doublesMatches['team2WonDoubles']; ?>
                </td>
                <td rowspan="2" class="align-middle">
                	<?php echo $gamesDoubles[0];?>
                </td>
                <td rowspan="2" class="align-middle">
                	<?php echo $gamesDoubles[1]; ?>
                </td>
                <td rowspan="2" class="align-middle">
                	<?php echo $pointsDoubles[0];?>
                </td>
                <td rowspan="2" class="align-middle">
                	<?php echo $pointsDoubles[1]; ?>
                </td>
                </tr>
                <tr>
                <td></td>
                <td class="align-left width14">Doubles 2</td>
                <?php for ($i=3;$i<6;$i++){?>
                <td  class="width7"> <input type="text" class="form-control inputwidth24" id="team1Points<?php echo $i;?>" name="team1Points<?php echo $i;?>" value="<?php echo $doubles[$i]->team1Points; ?>" /></td>
                <?php }?>
                <td></td>
                <td class="align-left width14">Doubles 2</td>
                <?php for ($i=3;$i<6;$i++){?>
                <td  class="width7"><input type="text" class="form-control inputwidth24" id="team2Points<?php echo $i;?>" name="team2Points<?php echo $i;?>" value="<?php echo $doubles[$i]->team2Points; ?>" /></td>
                <?php }?>
                </tr>
                <tr>
          			<td colspan="16"><button class="btn btn-default btn-lg" type="submit" name="submit">Submit</button></td>
                   
		  		</tr>
              </tbody>
            </table>
          </div>
        </div>
        </form>
      </div>
    </div>
  </div>
</div>
          <?php		
		  ob_flush();?>
</body>
</html>
